#!/bin/sh
#
# Copyright (C) 2000-2022 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Run a simple backup of a directory with many files multiple times
#  in // over multiple volumes, and run a restore for each Job.
#
TestName="many-incremental-test"
JobName=backup
. scripts/functions

scripts/cleanup
scripts/copy-test-confs
echo "$tmp/files" >${cwd}/tmp/file-list

$bperl -e 'add_attribute("$conf/bacula-sd.conf", "Maximum Concurrent Jobs", "50", "Device")'
$bperl -e 'add_attribute("$conf/bacula-sd.conf", "Maximum Concurrent Jobs", "50", "Storage")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "Maximum Concurrent Jobs", "50", "Storage")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "Maximum Concurrent Jobs", "50", "Director")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "Maximum Concurrent Jobs", "50", "Client")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "Maximum Concurrent Jobs", "50", "Job")'

$bperl -e 'add_attribute("$conf/bacula-sd.conf", "Maximum File Size", "5MB", "Device")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "SpoolData", "no", "Job")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "Accurate", "yes", "Job")'
director=`$bperl -e "get_dirname()"`

change_jobname NightlySave $JobName
start_test

$bperl -e 'create_many_files("$tmp/files", 300000, 10000)'

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log1.out
label storage=File volume=TestVolume001 drive=0 slot=0
messages
quit
END_OF_DATA

run_bacula

# compress the volume at the filesystem level if possible
chattr +c $tmp/TestVolume001 2> /dev/null

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out ${cwd}/tmp/log1.out
run job=$JobName yes
wait
messages
quit
END_OF_DATA

# run the full backup
run_bconsole

for i in `seq 1 30`
do
    $bperl -e 'update_some_files_rep("$tmp/files")'
    run_bconsole
done

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out ${cwd}/tmp/log2.out
restore select all done where=$tmp/bacula-restores
@exec "cp $working/$director.restore.1.bsr $working/restore1.bsr"
yes
wait
messages
quit
END_OF_DATA

run_bconsole

check_for_zombie_jobs storage=File
stop_bacula

$rscripts/diff.pl -s $tmp/files -d $tmp/bacula-restores/$tmp/files
dstat=$?

check_two_logs
end_test
